package com.y.controller;


import com.y.entity.Food;
import com.y.entity.Kind;
import com.y.entity.vo.DataTable;
import com.y.entity.vo.R;
import com.y.service.FoodService;
import com.y.service.KindService;
import com.y.utils.ImgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yjh
 * @since 2021-09-24
 */
@Controller
@RequestMapping("/food")
public class FoodController {

    @Autowired
    FoodService foodService;

    @Autowired
    KindService kindService;


    /**
     * 菜品分页
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public DataTable<Food> foodList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit",defaultValue = "10") Integer limit,
                                    @RequestParam(value = "name",defaultValue = "null",required = false) String name,
                                    @RequestParam(value = "kind",defaultValue = "null",required = false) String kind){

        return foodService.getFoodList(page,limit,name,kind);
    }

    /**
     * 添加食品
     * @param model
     * @return
     */
    @GetMapping("/addFoodPage")
    public String addFoodPage(Model model){
        List<Kind> list = kindService.query().eq("status","0").list();
        model.addAttribute("list",list);
        return "view/system/operate/food/add";
    }

    @PostMapping("/addFood")
    @ResponseBody
    public R addFood(Food food){
        System.out.println("food = " + food);
        return foodService.save(food) ? R.success().setMessage("添加了成功") : R.error().setMessage("添加失败");
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/uploadImage")
    @ResponseBody
    public R uploadImage(MultipartFile file){
        String avatar = ImgUtils.uploadImage(file);
        return R.success().setCode(0).setData(avatar);
    }


    /**
     * 获取单个食品
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/getOneFood/{id}")
    public String getOneFood(@PathVariable int id,Model model){
        Food food = foodService.getById(id);
        model.addAttribute("food",food);
        model.addAttribute("list",kindService.query().eq("status","0").list());
        return "view/system/operate/food/foodEdit";
    }

    @PostMapping("/updateFood")
    @ResponseBody
    public R updateFood(Food food){
        System.out.println("food = " + food);
        return foodService.updateById(food) ?
                R.success("更新了一条数据...") : R.error("更新失败...");
    }


    @DeleteMapping("/deleteFood/{id}")
    @ResponseBody
    public R deleteAnimal(@PathVariable int id){
        return foodService.removeById(id)?
                R.success("删除了一条数据..."): R.error("删除失败...");
    }



}
